
capture log close
log using $log/AED_dataprep_13_data_final, text replace

clear
set maxvar 10000
set matsize 800
set more off

set trace off
  
  
/* ------------------------------------------------------------------------------------ */
/* --- Generating the final dataset: Merging monthly outcome with control variables --- */
/* ------------------------------------------------------------------------------------ */

foreach iebversion in v901 {
		
		forvalue year = 2005/2008 {
		
				
				use $data/outcome_short_`year'_`iebversion', clear
				
				joinby iza_id using $data/xvars_`year'_`iebversion',unmatched(master)
				tab _merge
				drop _merge
				
				
				joinby iza_id using $data/history_`year'_`iebversion',unmatched(master)
				tab _merge
				drop _merge
				
				gen log_intensity_alt = log(intensity_uebergang_lag1 + intensity_ausruestung_lag1) 
				gen log_intensity_mobi = log(intensity_mobihilfe_lag1)
				
				destring wanderungen, replace
				destring bip, replace
				foreach var in share_agriculture share_industry share_service wanderungen bip {
				bys ba: egen `var'_avg=mean(`var')
				}
				
				
				gen iza_umzug12 = 1 if dur_umzug<=12
				replace iza_umzug12 = 0 if iza_umzug12==.
				rename iza_umzug iza_umzug24
				
				gen iza_commuting12 = 1 if dur_commuting<=12
				replace iza_commuting12 = 0 if iza_commuting12==.
				rename iza_commuting iza_commuting24
				
				gen west_aq_low = 1 if aloq<=0.06 & (ba>100 & ba<900)
				replace west_aq_low = 0 if west_aq_low==. 
				gen west_aq_high = 1 if aloq>0.06 & (ba>100 & ba<900)
				replace west_aq_high = 0 if west_aq_high==.
				
				gen east_aq_low = 1 if aloq<=0.14 & (ba<100|ba>900)
				replace east_aq_low = 0 if east_aq_low==. 
				gen east_aq_high = 1 if aloq>0.14 & (ba<100|ba>900)
				replace east_aq_high = 0 if east_aq_high==.
				
				
				#d;
				global samplevar "
				ba log_intensity_alt log_intensity_mobi vac_rate aloq    
				share_agriculture_avg share_industry_avg share_service_avg wanderungen_avg bip_avg
				sv_tplus_* ue_tplus_* geldsv_tplus_* geldleh_tplus_*
				iza_umzug12 iza_umzug24 iza_commuting12 iza_commuting24
				
				iza_famstd iza_kind iza_sex iza_abschluss_kat iza_ausb_kat iza_last_daily_income 
				iza_alter 
				iza_sv_tminus* iza_apprentice_tminus* sv_full_tminus* sv_part_tminus* sv_mid_tminus* 
				iza_lnwage_tminus* iza_ui_tminus* iza_alo_tminus* iza_olf_tminus*
				iza_treatment_tminus*
				iza_ende_last 
				iza_vztz_last 
				iza_zeit_la
				iza_beruf_kat 
				west_aq_low west_aq_high east_aq_low east_aq_high
				iza_ltt iza_stt iza_almp iza_ws
                iza_wo_kreis_ 
				bland_umzug
				aa_umzug
				beruf_kat
				sector_w03
				"
				;
				#d cr
				
				#d;
				keep iza_id	eintritt iza_eintrittd $samplevar
				kkz_neu
				kkz_alt
				dur_umzug
				umzug_dist_first
				;
				#d cr
				
				tostring eintritt, replace
				gen entry_m = substr(eintritt,5,2)
				destring entry_m eintritt, replace
				
				gen sector=1 if iza_beruf_kat==1
				replace sector=2 if (iza_beruf_kat==3|iza_beruf_kat==4)
				replace sector=3 if sector==.
				
				
				forvalues t=1/5 {
					replace iza_lnwage_tminus`t' = log(0.01) if iza_lnwage_tminus`t'==.
				}
				
				*Replace last missings as zero
				replace iza_ausb_kat = 0 if iza_ausb_kat==.
				replace iza_famstd = 0 if iza_famstd==.
				replace iza_zeit_la = 0 if iza_zeit_la==.
				
				gen iza_alter_kat1=1 if iza_alter<=25
				replace iza_alter_kat1=0 if iza_alter_kat1==.
				
				gen iza_alter_kat2=1 if iza_alter>25 & iza_alter<=35
				replace iza_alter_kat2=0 if iza_alter_kat2==.
				
				gen iza_alter_kat3=1 if iza_alter>35 & iza_alter<=45
				replace iza_alter_kat3=0 if iza_alter_kat3==.
				
				gen iza_alter_kat4=1 if iza_alter>45 & iza_alter<=55
				replace iza_alter_kat4=0 if iza_alter_kat4==.
				
				replace beruf_kat=0 if beruf_kat==.
				replace sector_w03=16 if sector_w03==.
				
				*Age restriction
				drop if iza_alter>55
				
				*Sample clean-up
				egen dropmissout=rowmiss($samplevar)
				drop if dropmissout>0
				
				*Exits 
				gen exit_sv_tplus_1 = sv_tplus_1
				forvalues t = 2/24 {
					local tminus1 = `t' -1
					di `tminus1'
					gen exit_sv_tplus_`t' = 1 if exit_sv_tplus_`tminus1'==1
					replace exit_sv_tplus_`t' = 1 if sv_tplus_`t' == 1
					replace exit_sv_tplus_`t' = 0 if exit_sv_tplus_`t'==.
				}
				
				*Hazards
				gen exit_tplus_1 = sv_tplus_1
				forvalues t = 2/24 {
					local tminus1 = `t' -1
					di `tminus1'
					gen exit_tplus_`t' = .
					replace exit_tplus_`t' = 1 if sv_tplus_`t' == 1 & exit_tplus_`tminus1'==0
					replace exit_tplus_`t' = 0 if sv_tplus_`t' == 0 & exit_tplus_`tminus1'==0
				}
				
				
				forvalues t = 1/24 {
					gen daily_income_tplus_`t' = geldsv_tplus_`t'
					replace daily_income_tplus_`t' = 0 if daily_income_tplus_`t'==.
				}
				
				
				
				gen year = `year'
				
				saveold $data/final_data_`year'_`iebversion', replace
				
					
				}/* year*/
			
		
		
}
	
log close 
			





